package com.woniu.communitymanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.entity.Owner;
import com.woniu.communitymanagement.service.OwnerService;
import com.woniu.communitymanagement.mapper.OwnerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author QIQI
* @description 针对表【owner(业主表)】的数据库操作Service实现
* @createDate 2022-11-29 16:59:44
*/
@Service
public class OwnerServiceImpl extends ServiceImpl<OwnerMapper, Owner>
    implements OwnerService{
    @Autowired(required = false)
    private OwnerMapper ownerMapper;
    @Override
    public HttpResult deleteById(Integer id) {
        HttpResult httpResult=null;

        int count = ownerMapper.deleteById(id);
        if(count>0){
            httpResult=new HttpResult(null,null,200,"删除成功");
        }else{
            httpResult=new HttpResult(null,null,500,"删除失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult updateOwnerById(Owner owner) {
        HttpResult httpResult=null;
        int count = ownerMapper.updateById(owner);
        if(count>0){
            httpResult=new HttpResult(null,null,200,"修改成功");
        }else{
            httpResult=new HttpResult(null,null,500,"修改失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult insertOwner(Owner owner) {
        HttpResult httpResult=null;
        int count = ownerMapper.insert(owner);
        if(count>0){
            httpResult=new HttpResult(null,null,200,"添加成功");
        }else{
            httpResult=new HttpResult(null,null,500,"添加失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectAll(String tel, String identity, Integer pageIndex, Integer pageSize) {
        HttpResult httpResult=null;
        List<Owner> owners = ownerMapper.selectHouseList(tel, identity, (pageIndex-1)*pageSize, pageSize);
        Integer count = ownerMapper.selectHouseCount(tel, identity);
        if(owners!=null&&owners.size()>0){
            httpResult=new HttpResult(owners,count,200,"查询成功");
        }else{
            if(pageIndex>1){
                owners = ownerMapper.selectHouseList(tel, identity, (pageIndex-1)*pageSize, pageSize);
                httpResult=new HttpResult(owners,count,200,"查询成功");
            }else{
                httpResult=new HttpResult(null,null,500,"查询失败");
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult selectOne(Integer id) {
        HttpResult httpResult=null;
        Owner owner = ownerMapper.selectOwnerById(id);
        if(owner!=null){
            httpResult=new HttpResult(owner,1,200,"查询成功");
        }else{
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult all() {
        HttpResult httpResult=null;
        List<Owner> owners = ownerMapper.all();
        if(owners!=null&&owners.size()>0){
            httpResult=new HttpResult(owners,owners.size(),200,"查询成功");
        }else {
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }
}




